#!/bin/bash

rini() {
    if [[ "$1" =~ ^[[:alpha:]]+$ ]]; then
        local data="$(cat - | sed 's/[;#].*$//;s/^\s*//;/^\s*$/d')"$'\n'"[]"
        local index="$(echo "$data" | grep -n -v '=')"
        local range=($(echo "$index" | grep -A1 '\b'$1'\b' | cut -d: -f1))
        if [ -z "$range" ]; then
            return 0
        fi
        data=$(echo "$data" | sed -n "$((${range[0]} + 1)),$((${range[1]} - 1))p")
        if [ -z "$2" ]; then
            echo "$data"
        else
            echo "$data" |
                awk -F '=' '$1~/^'$2'[ ]*$/{print $2}' |
                sed 's/^\s*['"'"'"]//;s/['"'"'"]\s*$//'
        fi
    else
        return 1
    fi
}

uini() {
    if [[ "$1" =~ ^[[:alpha:]]+$ ]]; then
        local data="$(cat - | sed '/^\s*$/d;s/^\s*//;s/^\([^[]\)/ \1/')"
        local index="$(echo -e "$data\n[]" | grep -n '^[[]')"
        local range=($(echo "$index" | grep -A1 '\b'$1'\b' | cut -d: -f1))
        case "$#" in
        1 | 2)
            echo "$data" | sed -n "1,${range[0]}p"
            echo "$2"
            echo "$data" | sed -n "${range[1]},\$p"
            ;;
        3)
            local n=($(echo "$data" |
                sed -n "${range[0]},${range[1]}{/^ $2\s*=/{=;p}}"))
            [ -n "$n" ] && { echo "$data" | sed "${n[0]}s/^[^#;]*/ $2=${3//\\/\\\\}/"; }
            ;;
        *) return 1 ;;
        esac
    else
        return 1
    fi
}

aini() {
    if [[ "$1" =~ ^[[:alpha:]]+$ ]]; then
        local data="$(cat - | sed '/^\s*$/d;s/^\s*//;s/^\([^[]\)/ \1/')"
        local index="$(echo -e "$data\n[]" | grep -n '^[[]')"
        local range=($(echo "$index" | grep -A1 '\b'$1'\b' | cut -d: -f1))
        case "$#" in
        1 | 2)
            [ -z "$range" ] && {
                echo "$data"
                echo "[ $1 ]"
                echo "$2"
            }
            ;;
        3)
            [ -n "$range" ] && {
                local n=($(echo "$data" |
                    sed -n "${range[0]},${range[1]}{/^ $2\s*=/{=;p}}"))
                [ -z "$n" ] && { echo "$data" | sed "${range[1]}i\\ $2=$3"; }
            }
            ;;
        *) return 1 ;;
        esac
    else
        return 1
    fi
}

dini() {
    if [[ "$1" =~ ^[[:alpha:]]+$ ]]; then
        local data="$(cat - | sed '/^\s*$/d;s/^\s*//;s/^\([^[]\)/ \1/')"
        local index="$(echo -e "$data\n[]" | grep -n '^[[]')"
        local range=($(echo "$index" | grep -A1 '\b'$1'\b' | cut -d: -f1))
        [ -n "$range" ] && {
            case "$#" in
            1)
                echo "$data" | sed "${range[0]},$((${range[1]} - 1))d"
                ;;
            2)
                local n=($(echo "$data" |
                    sed -n "${range[0]},${range[1]}{/^ $2\s*=/{=;p}}"))
                [ -n "$n" ] && { echo "$data" | sed "${n[0]}d"; }
                ;;
            *) return 1 ;;
            esac
        }
    else
        return 1
    fi
}

wini() {
    case "$#" in
    1) aini "$1" ;;
    2) [ -z "$2" ] && dini "$1" || aini "$1" "$2" ;;
    3) [ -z "$3" ] && dini "$1" "$2" || aini "$1" "$2" "$3" ;;
    esac
}